<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>编辑缺陷</title>
    <link href="../css/list-detial.css" type="text/css" rel="stylesheet"/>
</head>
<body>


<!--header-->
<header id="header" class="header">
    <div class="header-info left"><i></i><a href="javascript:" onclick="toDetail()">缺陷详情</a>&nbsp;>&nbsp;编辑缺陷</div>
    <div class="right-user right">
         <div class="clear t" >             <div id="headusername" class="role"></div>             <div class="icon"></div>         </div>
        <div class="head-down">
            <ul><li><a href="edit-pwd.html">修改密码</a></li><li><a onclick="loginOut()">退出系统</a></li></ul></div><div>
    </div>
    </div>
</header>
<!--menu-->
<aside id="aside" class="aside">
    <div class="logo">运营平台</div>
    <ul class="menu" id="asidemenu">
    </ul>
</aside>

<!--content-->
<div class="content">
    <div class="page-inner">
        <div class="page-content">
            <div class="table-head clear"><div class="left document-name"><i class="bug-i"></i></div><div class="right edit"><a onclick="returnList()" class="sure">取消</a><a onclick="commitEdit()" class="sure">保存</a></div></div>
            <div class="table-cont">
                <table class="content-table">
                    <tr>
                        <td><span class="red-start">*</span>缺陷主题：</td>
                        <td colspan="2"><input id="name" /></td>

                    </tr>
                    <tr>
                        <td>来源：</td>
                        <td>
                            <!--<select id="source"></select>-->
                            <input type="hidden" id="needSource"/>
                            <div class="exist-source clear" style="display: block" id="existSourceBox">
                                <div class="left" id="originalSourceName"></div><a class="left edit-source-btn" id="goEditSource">修改</a>
                            </div>
                            <div class="search-box clear" style="display: none" id="searchBox">

                                <input class="left" placeholder="输入至少两个字" />
                                <a class="left edit-source-btn" id="goSearchBtn">搜索</a>
                                <a class="left edit-source-btn" id="giveUp">放弃</a>
                                <select class="left" id="keyMatchSelect" onchange="_API.getComeSource('needSide',false,true,$(this).val())"><option>请输入关键词</option></select>
                            </div>
                        </td>
                        <td><span class="red-start">*</span>紧急程度：</td>
                        <td><select id="level"></select></td>
                    </tr>
                    <tr>
                        <td><span class="red-start">*</span>产品：</td>
                        <td><select id="product" onchange="_API.getFunctionList('functionList',false,false,'product')"></select></td>
                        <td><span class="red-start">*</span>功能模块:</td>
                        <td><select id="functionList"></select><span class="red-start" style="color: #4cae4c">先选择产品</span></td>
                    </tr>
                    <tr>
                        <td><span class="red-start">*</span>使用平台：</td>
                        <td><select id="plat"></select></td>
                        <td><span class="red-start">*</span>缺陷类型：</td>
                        <td><select id="issueType"></select></td>
                    </tr>

                    <tr>
                        <td>使用网络：</td>
                        <td><select id="networkType"></select></td>
                        <td>浏览器及其版本：</td>
                        <td><input id="browser"></td>
                    </tr>
                    <tr>
                        <td>缺陷状态：</td>
                        <td><select id="status"></select></td>
                        <td>优先级：</td>
                        <td><select id="priority"></select></td>
                    </tr>
                    <tr>
                        <td>手机及型号：</td>
                        <td colspan="3"><input id="mobileType"></td>
                    </tr>

                    <tr class="area">
                        <td><span class="red-start">*</span>缺陷描述：</td>
                        <td colspan="3">
                            <textarea id="discribe"></textarea>
                        </td>
                    </tr>
                    
                    <tr class="area">
                        <td>反馈信息：</td>
                        <td colspan="3">
                            <textarea id="remark"></textarea>
                        </td>
                    </tr>
                    <tr class="area">
                        <td>附件：</td>
                        <td colspan="3">
                            <a href="javascript:;" class="a-upload" id="fileUpload"><input id="file" value="请选择" type="file"/>点击这里上传</a>
                            <p class="format">支持的格式有：JPG，PNG，PDF，PPT，XLSX，XLS，CSV，DOCX，DOC，ZIP</p>
                            <p class="format">文件名不能包含*、#等特殊符号</p>
                            <ul class="addAttachmentList" id="addAttachmentList">
                            </ul>
                        </td>
                    </tr>
                </table>


            </div>

        </div>
    </div>
</div>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<script src="../js/common.js"></script>

<script type="text/javascript">


    var _id = getUrlId();
    function returnList(){
        location.href = 'pm-bug-detail.html?id='+_id;
    }

    function toDetail() {
        location.href = 'pm-bug-detail.html?id='+getUrlId();
    }

    //修改需求来源
    var goEditSource = $('#goEditSource');
    var searchBox = $('#searchBox');
    var existSourceBox = $('#existSourceBox');
    var giveUp = $('#giveUp');
    var goSearchBtn = $('#goSearchBtn');
    var keyMatchSelect = $('#keyMatchSelect');
    var needSource = $('#needSource');
    goEditSource.on('click',function () {
        searchBox.css({'display':'block'})
        existSourceBox.css({'display':'none'})
    })
    giveUp.on('click',function () {
        searchBox.css({'display':'none'});
        existSourceBox.css({'display':'block'});
        needSource.val(originalProjectId);
        keyMatchSelect.html('<option>请输入关键词</option>');
        $(this).siblings('input').val("")
    })
    goSearchBtn.on('click',function () {
        var _val = $(this).siblings('input').val();
        if(_val.length < 2) return;
        _API.checkProNameList('keyMatchSelect',0,true,_val)
    })
    keyMatchSelect.on('change',function () {
        needSource.val($(this).val());
    })

    // 上传文件
    var attachment = new attachment();
    attachment.init({
        fileUpload:$('#fileUpload'),
        addAttachmentList:$('#addAttachmentList')
    }).deleteFile();
    
    updata();
    function updata(){
        var url  = _ip+'/issue/getIssueById?callback=update&id='+_id;
        $.ajax({
            url:url,
            type: 'GET',
            dataType: 'jsonp',
            contentType:'application/json',
            jsonpCallback: 'update',
            success:function(data){
                if(data.success){
                    var data = data.data;
                    $('.document-name').append('ID.'+data.id)
                    $('#name').val(data.name);

                    _API.getIssueType('issueType',data.issueType)
//                        .getNeedSource('source',data.projectId)
//                        .getProNameList('source',data.projectId)
                        .getTypeList('typeList',data.type)
                        .getProductList('product',data.product)
                        .getPlatformList('plat',data.plat)
                            .getFunctionList('functionList',data.feature,false,false,data.product)
                        .getNetWorkList('networkType',data.networkType)
                        .getLevelList('level',data.level)
                        .getIssueStatusList('status',data.status)
                        .getPriorityList('priority',data.priority);
                    window.originalProjectId = data.projectId;
                    window.originalCustomerId = data.customerId;
                    $('#originalSourceName').html(data.projectName);
                    $('#needSource').val(data.projectId);
                    $('#browser').val(data.browser);
                    $('#mobileType').val(data.mobileType);
                    $('#discribe').val(data.discribe);
                    $('#scenario').html(data.scenario);
                    $('#nowSolution').html(data.nowSolution);
                    $('#sugSolution').html(data.sugSolution);
                    $('#remark').html(data.remark);

                    data.attachment && attachment.getAttachmentList(data.attachment.split(','));       

                }else{
                    alert(data.message);
                }
            }
        })
    }

    


    var needName = $('#needName');
        scenario = $('#scenario');
    needName.focus(function(){
        needName.removeClass('error-i');
    })
    scenario.focus(function(){
        scenario.removeClass('error-i');
    })

    // 提交编辑
    function commitEdit() {
        if(needName.val() == ''){
            needName.addClass('error-i');
            return
        } 
        if( scenario.val() == ''){
            scenario.addClass('error-i');
            return;
        }

        var attachment = [];
        var li = $('#addAttachmentList').find('li');
        if(li.length >= 1){
            li.each(function(){
                attachment.push( $(this).attr('rel') );
            })
        }

        var url  = _ip+'/issue/updateIssue'
        var data = {
            "id":_id,
            "name":$('#name').val(),
            "type":$('#issueType').val(),
            "networkType":$('#networkType').val(),
            "mobileType":$('#mobileType').val() || null,
            "product":$('#product').val(),
            "plat":$('#plat').val(),
            "feature":$('#functionList').val(),
            "browser":$('#browser').val() || null,
            "discribe":$('#discribe').val(),
            "remark":$('#remark').val(),
//            "creater":$.cookie('username'),
            "attachment":attachment.join(','),
            "status":$('#status').val(),
            "level":$('#level').val(),
            "priority":$('#priority').val(),
            "projectId":$('#needSource').val()
        }
        $.ajax({
            url:url,
            type : "post",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            dataType: 'json',
            contentType:'application/json',
            data:JSON.stringify(data),
            success:function(data){
                if(data.message === 'LOGIN') {
                    window.location.href = 'user-login.html';
                    return false;
                }
                if(data.success){

                    location.href = 'pm-bug-detail.html?id='+_id;
                }else{
                    alert(data.message);
                }
            }
        })
   }
</script>
</body>
</html>